const config = require('../../config')
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    task_count: 0,
    diary_count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  onShow: function() {
    var that = this
    var diary_count = wx.getStorageSync('diary_count')
    var task_count = wx.getStorageSync('task_count')
    that.setData({
      diary_count,
      task_count
    })
    if (!diary_count) {
      that.getDiaryCount()
    }
    if (!task_count) {
      that.getTaskCount()
    }
    wx.getStorage({
      key: 'refreshDiaryCount',
      success: function(res) {
        that.getDiaryCount()
        wx.removeStorage({
          key: 'refreshDiaryCount',
        })
      }
    })  
    
    wx.getStorage({
      key: 'refreshTaskCount',
      success: function(res) {
        that.getTaskCount()
        wx.removeStorage({
          key: 'refreshTaskCount',
        })
      }
    })
  },


  async getDiaryCount() {
    var that = this;
    const countResult = await db.collection('diary').count()
    const diary_count = countResult.total
    that.setData({
      diary_count
    })   
    wx.setStorageSync('diary_count', diary_count)
  },

  async getTaskCount() {
    var that = this;
    const countResult = await db.collection('taskmsg').count()
    const task_count = countResult.total
    that.setData({
      task_count
    })   
    wx.setStorageSync('task_count', task_count)
  },

  shareMessage: function() {

  },

  // 返回
  back: function() {
    wx.navigateBack({

    });
  }

})